/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.basic.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedRoadDTO;
import com.hopes.bizz.close.basic.dto.ClosedRoadExcelDTO;
import com.hopes.bizz.close.basic.entity.ClosedRoadEntity;
import com.hopes.bizz.close.basic.service.ClosedRoadService;
import com.hopes.bizz.close.basic.vo.ClosedHazardRoadExcelVO;
import com.hopes.bizz.close.basic.vo.ClosedRoadExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 车道信息
 *
 * @author pig
 * @date 2023-10-31 14:39:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/road" )
@Tag(description = "road" , name = "车道信息管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedRoadController {

    private final ClosedRoadService closedRoadService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedRoadDTO 车道信息
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('basic_road_view')" )
    public R getClosedRoadPage(Page page, ClosedRoadDTO closedRoadDTO) {
        LambdaQueryWrapper<ClosedRoadEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedRoadDTO.getRoadType()), ClosedRoadEntity::getRoadType, closedRoadDTO.getRoadType());
        wrapper.eq(StrUtil.isNotBlank(closedRoadDTO.getHazardFlag()), ClosedRoadEntity::getHazardFlag, closedRoadDTO.getHazardFlag());
        wrapper.like(StrUtil.isNotBlank(closedRoadDTO.getName()), ClosedRoadEntity::getName, closedRoadDTO.getName());
        return R.ok(closedRoadService.page(page, wrapper));
    }


    /**
     * 通过id查询车道信息
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('basic_road_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedRoadService.getById(id));
    }

    /**
     * 新增车道信息
     * @param closedRoad 车道信息
     * @return R
     */
    @Operation(summary = "新增车道信息" , description = "新增车道信息" )
    @SysLog("新增车道信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_road_add')" )
    public R save(@RequestBody ClosedRoadEntity closedRoad) {
        return R.ok(closedRoadService.save(closedRoad));
    }

    /**
     * 修改车道信息
     * @param closedRoad 车道信息
     * @return R
     */
    @Operation(summary = "修改车道信息" , description = "修改车道信息" )
    @SysLog("修改车道信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_road_edit')" )
    public R updateById(@RequestBody ClosedRoadEntity closedRoad) {
        return R.ok(closedRoadService.updateById(closedRoad));
    }

    /**
     * 通过id删除车道信息
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除车道信息" , description = "通过id删除车道信息" )
    @SysLog("通过id删除车道信息" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_road_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedRoadService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('basic_road_export')")
    public List<ClosedRoadExcelVO> template() {
        return closedRoadService.exportExcel(Collections.singletonList(new ClosedRoadEntity()));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/hazardRoadImport")
    @PreAuthorize("@pms.hasPermission('basic_road_export')")
    public List<ClosedHazardRoadExcelVO> hazardRoadImport() {
        return Collections.singletonList(new ClosedHazardRoadExcelVO());
    }


    /**
     * 导出excel 表格
     * @param closedRoadDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_road_export')")
    public List<ClosedRoadExcelVO> export(ClosedRoadDTO closedRoadDTO) {
        LambdaQueryWrapper<ClosedRoadEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedRoadDTO.getRoadType()), ClosedRoadEntity::getRoadType, closedRoadDTO.getRoadType());
        wrapper.eq(StrUtil.isNotBlank(closedRoadDTO.getHazardFlag()), ClosedRoadEntity::getHazardFlag, closedRoadDTO.getHazardFlag());
        wrapper.like(StrUtil.isNotBlank(closedRoadDTO.getName()), ClosedRoadEntity::getName, closedRoadDTO.getName());
        return closedRoadService.exportExcel(closedRoadService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param closedRoadExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('basic_road_export')")
    public R importExcel(@RequestExcel(ignoreEmptyRow = true) List<ClosedRoadExcelDTO> closedRoadExcelDtoList, BindingResult bindingResult) {
        return closedRoadService.importExcel(closedRoadExcelDtoList, bindingResult);
    }

    /**
     * 导入excel 表格
     * @param closedRoadExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/hazardRoadImport")
    @PreAuthorize("@pms.hasPermission('basic_road_export')")
    public R hazardRoadImport(@RequestExcel(ignoreEmptyRow = true) List<ClosedRoadExcelDTO> closedRoadExcelDtoList, BindingResult bindingResult) {
        for (ClosedRoadExcelDTO closedRoadExcelDTO : closedRoadExcelDtoList) {
            closedRoadExcelDTO.setRoadTypeStr("危险品车道");
        }
        return closedRoadService.importExcel(closedRoadExcelDtoList, bindingResult);
    }
}
